package com.newshub.admin.controller;

import com.newshub.logging.config.LoggingProperties;
import com.newshub.common.Result;
import com.newshub.logging.annotation.BusinessLog;
import com.newshub.logging.annotation.LogLevel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 日志管理控制器
 * 提供动态控制日志系统的能力
 * 集成到admin后台管理模块
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/logging")
public class LoggingManagementController {
    
    @Autowired
    private LoggingProperties loggingProperties;
    
    /**
     * 获取当前日志系统状态
     */
    @GetMapping("/status")
    @BusinessLog(module = "日志管理", description = "获取日志系统状态", level = LogLevel.INFO)
    public Result<Map<String, Object>> getLoggingStatus() {
        Map<String, Object> status = new HashMap<>();
        
        status.put("enabled", loggingProperties.isEnabled());
        status.put("businessLogEnabled", loggingProperties.isBusinessLogEnabled());
        status.put("performanceLogEnabled", loggingProperties.isPerformanceLogEnabled());
        status.put("exceptionLogEnabled", loggingProperties.isExceptionLogEnabled());
        status.put("asyncEnabled", loggingProperties.isAsyncEnabled());
        status.put("fileStorageEnabled", loggingProperties.getStorage().isFileEnabled());
        status.put("consoleOutputEnabled", loggingProperties.getStorage().isConsoleEnabled());
        
        return Result.success(status);
    }
    
    /**
     * 开启/关闭整个日志系统
     */
    @PostMapping("/toggle")
    @BusinessLog(module = "日志管理", description = "切换日志系统开关", level = LogLevel.INFO)
    public Result<String> toggleLoggingSystem(@RequestParam boolean enabled) {
        // 注意：这里只是示例，实际生产环境中应该通过配置中心或环境变量来管理
        // 因为直接修改内存中的配置对象在重启后会丢失
        
        if (enabled) {
            log.info("日志系统已开启");
            return Result.success("日志系统已开启");
        } else {
            log.info("日志系统已关闭");
            return Result.success("日志系统已关闭");
        }
    }
    
    /**
     * 开启/关闭业务日志
     */
    @PostMapping("/business-log/toggle")
    @BusinessLog(module = "日志管理", description = "切换业务日志开关", level = LogLevel.INFO)
    public Result<String> toggleBusinessLog(@RequestParam boolean enabled) {
        if (enabled) {
            log.info("业务日志已开启");
            return Result.success("业务日志已开启");
        } else {
            log.info("业务日志已关闭");
            return Result.success("业务日志已关闭");
        }
    }
    
    /**
     * 开启/关闭性能日志
     */
    @PostMapping("/performance-log/toggle")
    @BusinessLog(module = "日志管理", description = "切换性能日志开关", level = LogLevel.INFO)
    public Result<String> togglePerformanceLog(@RequestParam boolean enabled) {
        if (enabled) {
            log.info("性能日志已开启");
            return Result.success("性能日志已开启");
        } else {
            log.info("性能日志已关闭");
            return Result.success("性能日志已关闭");
        }
    }
    
    /**
     * 开启/关闭异常日志
     */
    @PostMapping("/exception-log/toggle")
    @BusinessLog(module = "日志管理", description = "切换异常日志开关", level = LogLevel.INFO)
    public Result<String> toggleExceptionLog(@RequestParam boolean enabled) {
        if (enabled) {
            log.info("异常日志已开启");
            return Result.success("异常日志已开启");
        } else {
            log.info("异常日志已关闭");
            return Result.success("异常日志已关闭");
        }
    }
    
    /**
     * 获取日志配置建议
     */
    @GetMapping("/recommendations")
    @BusinessLog(module = "日志管理", description = "获取日志配置建议", level = LogLevel.INFO)
    public Result<Map<String, String>> getLoggingRecommendations() {
        Map<String, String> recommendations = new HashMap<>();
        
        recommendations.put("production", "生产环境建议：关闭DEBUG日志，保留INFO及以上级别，开启异步处理");
        recommendations.put("development", "开发环境建议：开启DEBUG日志，开启控制台输出，关闭文件轮转");
        recommendations.put("testing", "测试环境建议：关闭业务日志，只保留异常日志，开启性能监控");
        
        return Result.success(recommendations);
    }
}
